﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Provider
{
    public class RoleProvider : GenericProvider, IRoleProvider
    {
        public IEnumerable<ApplicationRule> SearchRules(out int totalRecord, Guid applicationKey, string name = null, bool? active = null, int pagesize = 0, int pageindex = 0, bool isOutTotal = true)
        {
            var query = repository.GetQuery<ApplicationRule>().OrderBy(x => x.RuleId) as IQueryable<ApplicationRule>;

            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(x => x.RuleNameEnglish.ToUpper().Contains(name.Trim().ToUpper()));
            }
            if (active.HasValue)
            {
                var value = active.Value.ToBooleanString();
                query = query.Where(x => x.Active.ToUpper() == value);
            }

            if (isOutTotal)
            {
                totalRecord = query.Count();
            }
            else
            {
                totalRecord = -1;
            }

            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }

            return query.ToList();
        }


        public int Add(ApplicationRule role)
        {
            try
            {
                var applicationKey = role.ApplicationKey.ToUpper();
                var queryRules = repository.GetQuery<ApplicationRule>(x => x.ApplicationKey.ToUpper() == applicationKey);
                int maxId = 0;
                if (queryRules.Count() > 0) maxId = queryRules.Max(x => x.RuleId);
                maxId++;

                role.RuleId = maxId;
                repository.Add<ApplicationRule>(role);
                repository.UnitOfWork.SaveChanges();
                return maxId;
            }
            catch
            {
                return -1;
            }
        }


        public bool Update(ApplicationRule role)
        {
            try
            {
                var roleId = role.RuleId;
                var appKey = role.ApplicationKey.ToUpper();
                var roleCheck = repository.FindOne<ApplicationRule>(x => x.RuleId == roleId && x.ApplicationKey.ToUpper() == appKey);
                if (roleCheck == null) return false;

                roleCheck.Active = role.Active;
                roleCheck.Description = role.Description;
                roleCheck.RuleNameEnglish = role.RuleNameEnglish;
                roleCheck.RuleNameLocal = role.RuleNameLocal;

                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Delete(Guid applicationKey, int roleId)
        {
            try
            {
                var appKey = applicationKey.ToString().ToUpper();
                repository.Delete<ApplicationRule>(x => x.RuleId == roleId && x.ApplicationKey.ToUpper() == appKey);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
